import Ember from 'ember';
const {leaveStatus1,leaveStatus2,leaveStatus3,consultStatus6,consultStatus7} = Constants;
export default Ember.Controller.extend({
  dataLoader: Ember.inject.service("data-loader"),
  actions:{
    toStayBackDetail:function(customerId){
      var params = {};
      var mainpageController = App.lookup('controller:business.mainpage');
      if(customerId){
        this.store.findRecord('customerbusinessflow',customerId).then(function(customerflow){
          if(customerflow.get("leaveStatus").get("typecode")===Constants.leaveStatus2||customerflow.get("leaveStatus").get("typecode")===Constants.leaveStatus3){
            console.log("1111111111111111 editMode");
            params = {
              customerId:customerId,
              editMode:"look"
            };
          }else{
            console.log("1111111111111111 editModew2222222222");
            params = {
              customerId:customerId,
              editMode:"edit"
            };
          }
          mainpageController.switchMainPage('stay-back-detail',params);
        });
      }else {
        params = {
          editMode:"add",
        };
        mainpageController.switchMainPage('stay-back-detail',params);
      }
    },

    settlementSubmit(customer){//结算弹窗的  确认按键执行的方法
      App.lookup('controller:business.mainpage').openPopTip("正在结算");
      var leaveStatusObj = _self.get("dataLoader").findDict(Constants.leaveStatus3);
      customer.set("leaveStatus",leaveStatusObj);
      customer.save().then(function() {
          App.lookup('controller:business.mainpage').showPopTip("结算成功");
      });
		},
    chengeState:function(customer){
      var _self = this;
      var leaveStatusObj,
          leaveStatusTypecode = customer.get("leaveStatus").get("typecode");
      if(leaveStatusTypecode === Constants.leaveStatus1){//申请退住 变为 结算中
        // _self.store.query("dicttype",{filter:{typecode:Constants.leaveStatus2}}).then(function(dicttypes){
        //   var dicttype = dicttypes.get("firstObject");
        //   customer.set("leaveStatus",dicttype);
        //   customer.save().then(function(){
        //     _self.get('target').send('saveRefresh');
        //   });
        // });
        leaveStatusObj = this.get("dataLoader").findDict(Constants.leaveStatus2);
        customer.set("leaveStatus",leaveStatusObj);
        customer.save().then(function(){
          _self.get('target').send('saveRefresh');
        });
      }else if(leaveStatusTypecode === Constants.leaveStatus2){//结算中 变为 完成结算
        App.lookup('controller:business.mainpage').showConfirm("确定已完成该结算",function(){//加弹窗
          _self.send('settlementSubmit',customer);
        });

        _self.store.query("customer",{filter:{id:customer.get("id")}}).then(function(theCustomers){
          var theCustomer = theCustomers.get("firstObject");
          leaveStatusObj = _self.get("dataLoader").findDict(Constants.consultStatus6);//退住
          theCustomer.set("customerStatus",leaveStatusObj);
          theCustomer.save().then(function(){
            _self.get('target').send('saveRefresh');
          });
        });
      }
    },

    theToVoid:function(customer){
      var _self = this;
      App.lookup('controller:business.mainpage').showConfirm("是否确定作废此床位记录",function(){
        _self.send('cancelPassSubmit',customer);
      });
    },
    closeModel:function(){
      this.set("toVoidModal",false);
    },
    //弹窗确定，删除记录
    cancelPassSubmit:function(customer){
      App.lookup('controller:business.mainpage').openPopTip("正在作废");
			this.set("showpopInvitePassModal",false);
      var _self = this;
      _self.store.query("dicttype",{filter:{typecode:Constants.consultStatus7}}).then(function(dicttypes){
        var dicttype = dicttypes.get("firstObject");
          console.log("status11111111",customer.get("status").get("id"));
          customer.set("status",dicttype);
          customer.save().then(function(){
            App.lookup('controller:business.mainpage').showPopTip("作废成功");
            _self.get('target').send('saveRefresh');
          });
      });


    },

  },
});
